<?php

class ClientsController extends AppController {
    
    // define controller name
    var $name = 'Clients';
    
    // define list of models to use 
    var $uses = array('Client');
    
    // set controller domain
    var $controllerDomain = 'clients';
    
    public function beforeFilter() {
        parent::beforeFilter();
        
    }
    
    public function isAuthorized() {
        
        if (!$valid = parent::isAuthorized()) {
            return false;
        }
        
        return true;
    }
    
    public function create() {
        
        // get identification method
        $this->getClientIdTypes();
        
        // create new client
        if ($this->request->is('post')) {
            
            //check client id type and format
            //if (!$this->Client->checkIdType($this->request->data['Client']['personal_id_type'])) {
            //    $this->Client->invalidate('personal_id_type',__('Invalid id type'));
            //}
            
            if ($this->Client->sanitizeAll() && $this->Client->validate() && $this->Client->save($this->request->data)) {
                $this->Session->setFlash(__('Client created',true)); 
                $this->redirect(array('controller' => 'clients','action' => 'index')); 
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function delete($id = null) {
        
        if (!$this->Client->activeRecordExists($id)) {
            
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'clients','action' => 'index'));
            
        }

        $this->Client->delete($id);
        $this->Session->setFlash(__('Client deleted',true));
        $this->redirect(array('controller' => 'clients','action' => 'index'));      
    }
    
    public function edit($id = null) {
       
        // chek that record exists
        if (!$this->Client->activeRecordExists($id)) {   
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'clients','action' => 'index'));         
        }
        
        // get db record data
        $dbClient = $this->Client->find('first',array('conditions' => array('id' => $id, 'deletedOn' => null)));
        
        // get identification method
        $this->getClientIdTypes();
        
        // set data for modifications
        if ($this->request->is('get')) {
            $this->request->data = $dbClient;
        }
        
        // process modifications
        if ($this->request->is('post')){ 
            
            $this->Client->create();
            $this->Client->id = $id;
            
            //check client id type and format
            //if (!$this->Client->checkIdType($this->request->data['Client']['personal_id_type'])) {
            //    $this->Client->invalidate('personal_id_type',__('Invalid id type'));
            //}
            
            if ($this->Client->sanitizeAll() && $this->Client->validate() && $this->Client->save($this->request->data)) {
                $this->Session->setFlash(__('Update successfull',true));  
                $this->redirect(array('controller' => 'clients','action' => 'index'));             
            } 
               
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function view($id = null) {
        //todo
    }
    
    public function index() {
        //todo
    }
    
    public function get_items() {
        //todo
    }
    
    private function getClientIdTypes($set = true,$persist = true) {
        
        if ($set) $this->clientIdTypes = Constants::$clientIdTypes;
        if ($persist) $this->set('clientIdTypes',Constants::$clientIdTypes);
        
        return Constants::$clientIdTypes;
    }
    
}

?>